
package com.kugou.game.sdk.protocol;

import org.json.JSONObject;

import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;

/**
 * 描述:基本数据已经解析了
 * 
 * @author allenli
 * @since 2014-8-15 上午11:03:44
 */
public abstract class AbstractResponsePackage<T extends ResponseMessage> implements
        ResponsePackage<T> {

    byte[] data;

    @Override
    public void getResponseData(T response) {
        if (data != null && data.length > 0) {
            try {
                JSONObject jsonObj = new JSONObject(new String(data, "utf-8"));
                JSONObject jsonResp = jsonObj.getJSONObject("response");
                response.setCode(jsonResp.getString("code"));
                response.setMessage(jsonResp.getString("message"));
                response.setPrompt(jsonResp.getString("prompt"));
                if (response.isUseful()) {
                    parseResponse(response, jsonObj.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /** 解析响应数据 */
    public abstract void parseResponse(T response, String json);

    @Override
    public void setContext(byte[] arg0) {
        this.data = arg0;
    }

}
